home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_1 / easyprocess / source / launch / startproc.c < prev    next >
C/C++ Source or Header  |  1992-09-07  |  3KB  |  130 lines

  1. #include <arpbase.h>
  2. #include <arp_proto.h>
  3. #include "Launch.h"
  4. #include "LaunchPriv.h"
  5.  
  6. struct List *ProcPairList;
  7.  
  8. /*
  9.  *    NAME
  10.  *        StartProcess -- start a new process.
  11.  *
  12.  *    SYNOPSIS
  13.  *        Child = StartProcess (Name, Entry, Data)
  14.  *
  15.  *        struct Process *StartProcess (char *, CPTR, void *);
  16.  *
  17.  *    FUNCTION
  18.  *        Create a process to start at the entry point specified and
  19.  *        pass it the data.
  20.  *
  21.  *    DESCRIPTION
  22.  *        We check if the process pair list already exists, if not we
  23.  *        create it.  We then allocate a ProcPair structure to hold all
  24.  *        the data needed for the control:
  25.  *
  26.  *            Both processes' structure pointer, the signal bits,
  27.  *            the entry point and data for the child.
  28.  *
  29.  *        We then try to start the child.  If successful, we add the
  30.  *        process pair to the list.
  31.  *
  32.  *        In order not to have too many signal bit allocated, we always
  33.  *        check if the parent has already launched another child which
  34.  *        is still active and use the same signal bit.
  35.  *
  36.  *    INPUT
  37.  *        Name - the name for the new process, does NOT need to be unique.
  38.  *        Entry - the entry point (function) for the enw process.
  39.  *        Data - the data (anything) to be passed as an argument to the
  40.  *            entry point of the new process.
  41.  *
  42.  *    OUTPUT
  43.  *        Proc - the new process' structure pointer.
  44.  *
  45.  *    NOTE
  46.  *        We use arp.library, but it can be adapted easily to 2.04.
  47.  *
  48.  *    HISTORY
  49.  *        1992/09/06    Pierre Baillargeon        Creation
  50.  *
  51.  *    SEE ALSO
  52.  *        KillProcess(), KillProcesses(), WaitProcess(), WaitProcesses()
  53.  */
  54.  
  55. struct Process *StartProcess (char *Name, CPTR EntryPoint, void *Data, WORD Priority)
  56. {
  57.     struct ProcPair *pp = NULL;
  58.     struct ProcessControlBlock    PCB = { 8192L, 0,
  59.                                         PRF_CODE | PRF_NOCLI | PRF_SAVEIO,
  60.                                         0, 0, 0, 0, ChildEntry, 0, 0 };
  61.  
  62.     Forbid ();
  63.  
  64.     /*
  65.      *    Create the list if it does not exist.
  66.      */
  67.  
  68.     if (NULL == ProcPairList)
  69.     {
  70.         ProcPairList = (struct List *)AllocMem (sizeof (struct List), MEMF_CLEAR);
  71.         if (NULL == ProcPairList)
  72.         {
  73.             goto Error;
  74.         }
  75.         NewList (ProcPairList);
  76.     }
  77.  
  78.     /*
  79.      *    Create and initialize the process pair.
  80.      */
  81.  
  82.     pp = (struct ProcPair *)AllocMem (sizeof (struct ProcPair), MEMF_CLEAR);
  83.     if (NULL == pp)
  84.     {
  85.         goto Error;
  86.     }
  87.  
  88.     pp->pp_Node.ln_Type = 0;
  89.     pp->pp_Node.ln_Pri = 0;
  90.     pp->pp_Parent = (struct Process *)FindTask (NULL);
  91.  
  92.     /*
  93.      *    Try to find if this process has already launched another and
  94.      *    use the same signal bit.
  95.      */
  96.  
  97.     if (-1L == (pp->pp_ParentBit = AllocParentSignal ()))
  98.     {
  99.         goto Error;
  100.     }
  101.  
  102.  
  103.     /*
  104.      *    Launch child, if successful add the process pair to the list.
  105.      */
  106.  
  107.     pp->pp_ChildEntry = (void (*__saveds)(void *))EntryPoint;
  108.     pp->pp_ChildData = Data;
  109.     PCB.pcb_Pri = Priority;
  110.  
  111.     if (ASyncRun (Name, NOCMD, &PCB) >= 0L)
  112.     {
  113.         pp->pp_Child = (struct Process *)(((char *)PCB.pcb_WBProcess) - sizeof (struct Task));
  114.         AddHead (ProcPairList, &pp->pp_Node);
  115.         Signal (pp->pp_Child, 1L << pp->pp_Child->pr_MsgPort.mp_SigBit);
  116.         Permit ();
  117.         return (struct Process *)(((char *)PCB.pcb_WBProcess) - sizeof (struct Task));
  118.     }
  119.  
  120. Error:
  121.     if (pp)
  122.     {
  123.         FreeParentSignal (pp->pp_ParentBit);
  124.         FreeMem (pp, sizeof (struct ProcPair));
  125.     }
  126.     FreeProcPairList ();
  127.     Permit ();
  128.     return NULL;
  129. }
  130.